<template>
	<div style="height:3000px">
		<!-- 工单领料 -->

			<v-card>
				<v-card-title>
					<v-row >
						<v-col cols="2">
							<v-btn class="ml-2"  tile color="primary" @click.stop="AddDialog()">
								新增
							</v-btn>
							<v-btn class="ml-2"  tile color="deep-orange" @click.stop="deleteOrder()">
								删除
							</v-btn>
						</v-col>
						<v-col cols="8" >
								<v-row >
									<v-col cols="3" >
										<v-text-field label="物料代码" v-model="Sitemcode" placeholder="物料代码" dense outlined>
										</v-text-field>
									</v-col>
										
									<v-col cols="3" >
										<v-text-field label="物料名称" v-model="Sitemname" placeholder="物料名称" dense outlined>
										</v-text-field>
									</v-col>
								
									
									<v-col cols="3">
										<v-select  v-model="flagselect" :items="flagitems"
															item-text="name"
										          item-value="value"
											solo dense label="是否可用">
										</v-select>
									</v-col>
									
									<v-col cols="3">
										<v-btn depressed color="primary" @click="searchinfo()" tile>查询</v-btn>
									</v-col>
								</v-row>																												
						</v-col>
						
						<v-col cols="2" align="end">
							<v-btn dark color="blue lighten-3" @click="refreshApps">
								<v-icon dark>mdi-refresh-circle</v-icon>刷新
							</v-btn>
						</v-col>
						
					</v-row>					
				</v-card-title>
				
				<v-alert dense text color="blue-grey"></v-alert>
				
				<v-data-table :headers="HeaderList" :items="DataList" small dense
					fixed-header item-key="id"  class="text-no-wrap" show-select  v-model="selected"
					loading-text="正在加载请稍等......" :loading="OrderLoading"
					:options.sync="OrderOptions" :footer-props="vuetifyDefaultPage"
					:server-items-length="OrderTotalCount">
					<template v-slot:item.enable="{ item }">
						<v-chip v-if="item.enable == 0" color="error" small label
							
						>
							禁用
						</v-chip>
						<v-chip v-else color="success" small label
							
						>
							可用
						</v-chip>
					</template>
					<template v-slot:item.actions="{ item }">
			      <v-btn tile color="warning" small @click.stop="EditDialog(item)">
							<v-icon left>
								mdi-pencil
							</v-icon>
							编辑
			      </v-btn>
			    </template>
				</v-data-table>
				<v-row justify="center">
					<v-col cols="8">
						<v-container class="max-width">
							<v-pagination v-model="pageModel.pagination.pageNumber" class="my-4"
								:length="pageModel.pagination.totalPages" :total-visible="10"
								prev-icon="mdi-menu-left" next-icon="mdi-menu-right" circle @input="goPage">
							</v-pagination>
						</v-container>
					</v-col>
				</v-row>
			</v-card>
	
	

		<!-- 对话框 -->
		<v-dialog v-model="AddOrEditDialog" max-width="1000px">
			<v-card>
				<v-card-title class="text-h5">
				  {{DialogHeader}}
				</v-card-title>
				<v-card-text>
				  <div class="mb-12">
				  	<v-row dense>
				  		<v-col cols="12">
				  			<v-row>
									<v-col cols="6">
										<v-text-field label="物料代码"  clearable :rules="[rules.required]"
											v-model="DialogBill.itemcode">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="物料名称"  clearable :rules="[rules.required]"
											v-model="DialogBill.itemname">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-select  v-model="DialogBill.type" :items="Stype"
															item-text="name"
										          item-value="value"
											 label="类型">
										</v-select>
									</v-col>
									<v-col cols="6">
										<v-text-field label="仓库编号"  clearable :rules="[rules.required]"
											v-model="DialogBill.warehouse">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="库区编号"  clearable :rules="[rules.required]"
											v-model="DialogBill.zonecode">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="包装数量"  clearable :rules="[rules.required]"
											v-model="DialogBill.package">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="长"  clearable :rules="[rules.required]"
											v-model="DialogBill.length">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="宽"  clearable :rules="[rules.required]"
											v-model="DialogBill.width">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="高"  clearable :rules="[rules.required]"
											v-model="DialogBill.height">
										</v-text-field>
									</v-col>
									<v-col cols="6">
										<v-text-field label="重量"  clearable :rules="[rules.required]" 
											v-model="DialogBill.weight">
										</v-text-field>
									</v-col>
				  				<v-col cols="6">
										<v-select  v-model="DialogBill.enable" :items="Senable"
															item-text="name"
										          item-value="value"
											 label="是否可用">
										</v-select>
				  				</v-col>
				  			</v-row>
				  		</v-col>
				  	</v-row>
				  </div>
				</v-card-text>
				<v-card-actions>
				  <v-spacer></v-spacer>
					
				  <v-btn
				    color="green darken-1"
				    text
				    @click="confirmOrder()"
				  >
				    提交
				  </v-btn>
					
				  <v-btn
				    color="green darken-1"
				    text
				    @click="AddOrEditDialog = false"
				  >
				    关闭
				  </v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
	</div>
</template>

<script>
export default {
  name: 'wms_baseinfo_master',
  data: () => ({
		pageModel: {
			loading: true,
			pagination: {
				pageNumber: 1,
				pageSize: 10,
				//总页数
				totalPages: 0,
				//总记录数
				totalRecords: 0
			}
		},
		  Sitemcode:'',
			Sitemname:'',
			flagselect:-1,
			flagitems:[
				{name: '所有', value:-1},
				{name: '禁用', value:0},
				{name: '可用', value:1},
			],
			Senable:[
				{name: '禁用', value:0},
				{name: '可用', value:1},
			],
			Stype:[
				{name: '原材料', value:1},
				{name: '半成品', value:2},
				{name: '成品', value:3},
			],
  		searchInput: '',
  		AddOrEditDialog: false,
  		HeaderList: [
				{ text: '物料编号',value: 'itemcode'},
				{ text: '物料名称', value: 'itemname' },
				{ text: '物料类型', value: 'type' },
  			{ text: '仓库编号',value: 'warehouse'},
  			{ text: '库区编号', value: 'zonecode' },
				{ text: '包装数量', value: 'package' },
  			{ text: '长', value: 'length' },
				{ text: '宽', value: 'width' },
				{ text: '高', value: 'height' },
				{ text: '重量', value: 'weight' },
				{ text: '是否可用', value: 'enable' },
				{ text: '创建人', value: 'created_byname' },
				{ text: '创建时间', value: 'created_time' },
				{ text: '编辑人', value: 'updated_byname' },
				{ text: '编辑时间', value: 'updated_time' },
				{ text: '操作', value: 'actions' },
  		],
			DialogBill:{
				id:'',
				dbid:'',
				isavailable:true,
				created_by:'',
				created_byname:'',
				updated_by:'',
				updated_byname:'',
				company:'',
				created_time:'',
				updated_time:'',
				accountingbookcode:'',	
						
				itemcode:'',
				itemname:'',	
				type:1,
				warehouse:'',
				zonecode:'',
				package:'',
				length:0,		
				width:0,		
				height:0,		
				weight:0,		
				enable:1,
			},
  		DataList: [],
			selected:[],
  		checkList: [],
  		vuetifyDefaultPage: {
  			'items-per-page-text': '每页个数',
  			'items-per-page-options': [10, 20, 50, 100]
  		},
  		OrderLoading: true,
  		OrderTotalCount: 0,
  		OrderOptions: {
  			//页数
  			page: 1,
  			//每页行数
  			itemsPerPage: 10
  		},
			DialogHeader:"",
  		leftCheckMaterialValid: false,
  		//领料单 表单信息
  		leftMaterialRequestOrderModel: {},
  		leftMaterialValid: true,
  		//领料单 需要数量规则
  		rules: {
  		    required: value => !!value || '值不能为空！',
  		    counter: value => value.length <= 20 || '字符长度不能超过20!',
  				num: value => value > 0 || '值要大于0！',
  				true: value => {
  					const pattern = /^[0-9]*$/
  					return pattern.test(value) || '请输入合法数字!'
  				}
  		 }
  	}),
  
  	created() {  
			//获取单据信息
  		this.loadDataList()
  	},
  	methods: {
  		//搜索
  		search() {
  			this.loadCuttingDetailList()
  		},
  		//加载单据信息
  		loadDataList() {
  			var that = this;
				that.OrderLoading = true;
  			this.$httpRequest({
  				url: `WMS/WmsBaseMaster/GetBillListByPage?pageIndex=${this.OrderOptions.page}&pageSize=${this.OrderOptions.itemsPerPage}`,
  				method: 'get'
  			}).then(function(response) {
  				if (response.status == 1) {
						console.log(response)
  					that.DataList = response.data.result;
						that.OrderTotalCount = response.data.pagination.totalRecords;
						that.OrderLoading = false;
  				} else {
  					console.log('加载单据信息失败');
  				}
  			})
  		},
  		// 打开编辑对话框
  		EditDialog(item) {
				this.DialogBill.id = item.id;
				this.DialogBill.dbid = item.dbid;
				this.DialogBill.isavailable = item.isavailable;
				this.DialogBill.created_by = item.created_by;
				this.DialogBill.created_byname = item.created_byname;
				this.DialogBill.updated_by = item.updated_by;
				this.DialogBill.updated_byname = item.updated_byname;
				this.DialogBill.company = item.company;
				this.DialogBill.created_time = item.created_time;
				this.DialogBill.updated_time = item.updated_time;
				
				this.DialogBill.itemcode = item.itemcode;
				this.DialogBill.itemname = item.itemname;
				this.DialogBill.type = item.type;
				this.DialogBill.warehouse = item.warehouse;
				this.DialogBill.zonecode = item.zonecode;
				this.DialogBill.package = item.package;
				this.DialogBill.length = item.length;
				this.DialogBill.width = item.width;
				this.DialogBill.height = item.height;
				this.DialogBill.weight = item.weight;
				this.DialogBill.enable = item.enable;
				this.DialogHeader = "编辑物料";
				this.AddOrEditDialog = true;
  		},
			// 打开新增对话框
			AddDialog() {
				this.DialogBill.itemcode = "";
				this.DialogBill.itemname = "";
				this.DialogBill.type = 1;
				this.DialogBill.warehouse = "";
				this.DialogBill.zonecode = "";
				this.DialogBill.package = 0;
				this.DialogBill.length = 0;
				this.DialogBill.width = 0;
				this.DialogBill.height = 0;
				this.DialogBill.weight = 0;
				this.DialogBill.enable = 1;
				this.DialogHeader = "新增物料";
				this.AddOrEditDialog = true;
			},
			//分页跳转
			goPage(){
				this.loadDataList();
				that.pageModel.loading = true;
			},
  		//提交
  		confirmOrder() {
  			var that = this;
				if(that.DialogHeader == "新增物料")
				{
					this.$httpRequest({
						url: `/WMS/WmsBaseMaster/Add`,
						method: 'post',
						data:{			
								itemcode : that.DialogBill.itemcode,
								itemname : that.DialogBill.itemname,
								type : that.DialogBill.type,
								warehouse : that.DialogBill.warehouse,
								zonecode : that.DialogBill.zonecode,
								package : that.DialogBill.package,
								length : that.DialogBill.length,
								width : that.DialogBill.width,
								height : that.DialogBill.height,
								weight : that.DialogBill.weight,
								enable : that.DialogBill.enable,
						}
					}).then(function(response) {
					
						if (response.status == 1) {
							that.loadDataList();
							that.AddOrEditDialog = false;
						} else {
							console.log('新建信息失败');
						}
					})
				}
				else
				{
					this.$httpRequest({
						url: `/WMS/WmsBaseMaster/Update`,
						method: 'post',
						data:{		
								id : that.DialogBill.id,
								dbid : that.DialogBill.dbid,
								isavailable : that.DialogBill.isavailable,
								created_by : that.DialogBill.created_by,
								created_byname : that.DialogBill.created_byname,
								updated_by : that.DialogBill.updated_by,
								updated_byname : that.DialogBill.updated_byname,
								company : that.DialogBill.company,
								created_time : that.DialogBill.created_time,
								updated_time : that.DialogBill.updated_time,
								
								itemcode : that.DialogBill.itemcode,
								itemname : that.DialogBill.itemname,
								type : that.DialogBill.type,
								warehouse : that.DialogBill.warehouse,
								zonecode : that.DialogBill.zonecode,
								package : that.DialogBill.package,
								length : that.DialogBill.length,
								width : that.DialogBill.width,
								height : that.DialogBill.height,
								weight : that.DialogBill.weight,
								enable : that.DialogBill.enable,
						}
					}).then(function(response) {
					
						if (response.status == 1) {
							that.loadDataList();
							that.AddOrEditDialog = false;
						} else {
							console.log('编辑信息失败');
						}
					})
				}
  			
  		},
  		//取消 取消领料单
  		cancelOrder() {
  			this.AddOrEditDialog = false;
  		},
			deleteOrder(){
				var that = this;
				for(var i=0; i<that.selected.length; i++)
				{
					this.$httpRequest({
						url: `/WMS/WmsBaseMaster/Delete/${that.selected[i].id}`,
						method: 'delete'											
					}).then(function(response) {					
						if (response.status == 1) {							
							that.$message.success("删除成功！");							
						} else {
							that.$message.error("删除失败！");
						}
					})
				}
				that.loadDataList();
			},
			searchinfo(){
				console.log(this.flagselect);
				var that = this
				this.$httpRequest({
					url: `WMS/WmsBaseMaster/Search?Sitemcode=${this.Sitemcode}&Sitemname=${this.Sitemname}&Senable=${this.flagselect}&pageIndex=${this.pageModel.pagination.pageNumber}&pageSize=${this.pageModel.pagination.pageSize}`,
					method: 'get'
				}).then(function(response) {
					if (response.status == 1) {
						that.DataList = response.data.result;
						that.pageModel.pagination.totalPages = response.data.pagination.totalPages;
						that.pageModel.pagination.totalRecords = response.data.pagination.totalRecords;
						that.pageModel.loading = false;
					} else {
						console.log('加载单据信息失败');
					}
				})
			},
			refreshApps(){
				this.Sitemcode = "";
				this.Sitemname = "";
				this.flagselect = -1;
				this.selected = [];
				this.OrderOptions.page = 1;
				this.loadDataList();
			},
			
  	}
  
  }

</script>

<style>
	$select-small-chips-dense-selections-min-height{
		height:40px;
	}
</style>
